﻿//-----------------------------------------------------------------------
// <copyright file="TreasureMapPage.xaml.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------
namespace Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.Views
{
    using System.Diagnostics;
    using Microsoft.ALMRangers.VsarTreasureMap.WindowsStoreApp.MVVMSupport;
    using Windows.System;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Input;

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class TreasureMapPage : Page, IPageState
    {
        /// <summary>
        /// The control pressed
        /// </summary>
        private bool controlPressed;

        /// <summary>
        /// Initializes a new instance of the TreasureMapPage class.
        /// </summary>
        public TreasureMapPage()
        {
            this.InitializeComponent();
            VisualStateManager.GoToState(this, "ZoomedIn", false);
        }

        /// <summary>
        /// Gets the state of a page.
        /// </summary>
        /// <returns>The page state information</returns>
        public object GetState()
        {
            var state = new TreasureMapPageState();
            state.HorizontalOffset = ZoomedInScrollViewer.HorizontalOffset;
            state.ZoomedIn = VisualStateGroup.CurrentState.Name == "ZoomedIn";
            return state;
        }

        /// <summary>
        /// Sets the state of a page.
        /// </summary>
        /// <param name="state">The state information.</param>
        public void SetState(object state)
        {
            var pageState = state as TreasureMapPageState;
            if (!pageState.ZoomedIn)
            {
                // we are zoomed in by default, so only need to handled zoomed out
                VisualStateManager.GoToState(this, "ZoomedOut", false);
            }
            else
            {
                ZoomedInScrollViewer.UpdateLayout();
                ZoomedInScrollViewer.ScrollToHorizontalOffset(pageState.HorizontalOffset);
            }
        }

        /// <summary>
        /// Zooms the in.
        /// </summary>
        private void ZoomIn()
        {
            VisualStateManager.GoToState(this, "ZoomedIn", true);
        }

        /// <summary>
        /// Zooms the out.
        /// </summary>
        private void ZoomOut()
        {
            VisualStateManager.GoToState(this, "ZoomedOut", true);
        }

        /// <summary>
        /// Zooms the button click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void ZoomButtonClick(object sender, RoutedEventArgs e)
        {
            this.ZoomOut();
        }

        /// <summary>
        /// Zooms the pointer wheel changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="PointerRoutedEventArgs"/> instance containing the event data.</param>
        private void ZoomPointerWheelChanged(object sender, PointerRoutedEventArgs e)
        {
            if (e.KeyModifiers == VirtualKeyModifiers.Control)
            {
                if (e.GetCurrentPoint(this).Properties.MouseWheelDelta > 0)
                {
                    this.ZoomIn();
                }
                else
                {
                    this.ZoomOut();
                }

                e.Handled = true;
            }
        }

        /// <summary>
        /// Handles the ManipulationDelta event of the Grid control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ManipulationDeltaRoutedEventArgs"/> instance containing the event data.</param>
        private void Grid_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            if (e.Cumulative.Scale < 1)
            {
                this.ZoomOut();
            }
            else
            {
                this.ZoomIn();
            }

            e.Handled = true;
        }

        /// <summary>
        /// Handles the KeyDown event of the Grid control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="KeyRoutedEventArgs"/> instance containing the event data.</param>
        private void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            switch ((int)e.Key)
            {
                case 17:
                    {
                        this.controlPressed = true;
                        break;
                    }

                case 187: // plus/equals
                case 107: // add
                    {
                        if (this.controlPressed)
                        {
                            this.ZoomIn();
                            e.Handled = true;
                        }

                        break;
                    }

                case 189: // minus/dash
                case 109: // subtract on key pad
                    {
                        if (this.controlPressed)
                        {
                            this.ZoomOut();
                            e.Handled = true;
                        }

                        break;
                    }
            }
        }

        /// <summary>
        /// Handles the KeyUp event of the Grid control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="KeyRoutedEventArgs"/> instance containing the event data.</param>
        private void Grid_KeyUp(object sender, KeyRoutedEventArgs e)
        {
            switch (e.Key)
            {
                case VirtualKey.Control:
                    {
                        this.controlPressed = false;
                        break;
                    }
            }
        }

        /// <summary>
        /// Class TreasureMapPageState
        /// </summary>
        private class TreasureMapPageState
        {
            /// <summary>
            /// Gets or sets a value indicating whether [zoomed in].
            /// </summary>
            /// <value><c>true</c> if [zoomed in]; otherwise, <c>false</c>.</value>
            public bool ZoomedIn { get; set; }

            /// <summary>
            /// Gets or sets the horizontal offset.
            /// </summary>
            /// <value>The horizontal offset.</value>
            public double HorizontalOffset { get; set; }
        }
    }
}
